package com.ujiuye.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo10Servlet")
public class Demo10Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //写回去一个json对象
//        String json = "{\n" +
//                "\t\"name\": \"zhangsan\",\n" +
//                "\t\"age\": 17,\n" +
//                "\t\"sex\": \"男\"\n" +
//                "}";


        //写回去一个json数组
        //String json = "[\"张三\", \"李四\", \"王二麻子\", \"王五\"]";


        //写回去混合模式的json
        String json = "[{\n" +
                "\t\"name\": \"张三\",\n" +
                "\t\"age\": 17\n" +
                "}, {\n" +
                "\t\"name\": \"张三1\",\n" +
                "\t\"age\": 127\n" +
                "}, {\n" +
                "\t\"name\": \"张三2\",\n" +
                "\t\"age\": 117\n" +
                "}]";


        resp.getWriter().print(json);
    }
}
